package com.astudio.lhasa;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.astudio.lhasa.model.City;
import com.astudio.lhasa.model.Image;
import com.astudio.lhasa.model.ImageDao;
import com.astudio.lhasa.model.TourSpot;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

public class TourSpotDetailActivity extends AppCompatActivity {
    public final static String TOUR_EXTRA = "tour_spot_info";
    private static final String TAG = "TourSpotDetailActivity";
    SimpleDraweeView image0;
    SimpleDraweeView image1;
    SimpleDraweeView image2;
    SimpleDraweeView image3;
    SimpleDraweeView image4;
    TextView summary;
    TextView name;
    TextView contact;
    TextView ticketInfo;

    TourSpot spot;
    City city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tour_spot_detail);
        summary = findViewById(R.id.summary);
        name = findViewById(R.id.name);
        contact = findViewById(R.id.contact);
        ticketInfo = findViewById(R.id.ticketInfo);

        image0 = findViewById(R.id.image0);
        image1 = findViewById(R.id.image1);
        image2 = findViewById(R.id.image2);
        image3 = findViewById(R.id.image3);
        image4 = findViewById(R.id.image4);

        city = getIntent().getParcelableExtra(CityDetail.CITY_EXTRA);
        spot = getIntent().getParcelableExtra(TOUR_EXTRA);

        List<Image> images = MyApplication
                .getDaoSession()
                .getImageDao()
                .queryBuilder()
                .where(ImageDao.Properties.Owner.eq(spot.getId()))
                .limit(5).list();
        if (!images.isEmpty()) {
            try {
                int end = images.get(0).getUrl().indexOf("imageMogr2");
                GenericDraweeHierarchyBuilder builder =
                        new GenericDraweeHierarchyBuilder(getResources());
                GenericDraweeHierarchy hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                image0.setHierarchy(hierarchy);
                image0.setImageURI(images.get(0).getUrl().substring(0, end));
                end = images.get(1).getUrl().indexOf("imageMogr2");
                hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                image1.setHierarchy(hierarchy);
                image1.setImageURI(images.get(1).getUrl().substring(0, end));
                end = images.get(2).getUrl().indexOf("imageMogr2");
                hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                image2.setHierarchy(hierarchy);
                image2.setImageURI(images.get(2).getUrl().substring(0, end));
                end = images.get(3).getUrl().indexOf("imageMogr2");
                hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                image3.setHierarchy(hierarchy);
                image3.setImageURI(images.get(3).getUrl().substring(0, end));
                end = images.get(4).getUrl().indexOf("imageMogr2");
                hierarchy = builder
                        .setFadeDuration(300)
                        .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                        .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.CENTER_CROP)
                        .build();
                image4.setHierarchy(hierarchy);
                image4.setImageURI(images.get(4).getUrl().substring(0, end));
            } catch (IndexOutOfBoundsException e) {
                Log.e(TAG, "onCreate: ", e);
            }
        }

        name.setText(spot.getName());
        if (spot.getSummary() != null) {
            summary.setText(spot.getSummary().replaceFirst("^\\s+", ""));
        } else {
            summary.setText("无信息");
        }
        contact.setText(spot.getContact() == null ? "无信息" : spot.getContact());
        if (spot.getTicketInfo() != null) {
            ticketInfo.setText(spot.getTicketInfo().replaceFirst("^\\s+", ""));
        } else {
            ticketInfo.setText("无信息");
        }

        setTitle(spot.getName());

        View moreImagesBtn = findViewById(R.id.more_pics);
        moreImagesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TourSpotDetailActivity.this, TourSpotImagesActivity.class);
                intent.putExtra(TOUR_EXTRA, spot);
                intent.putExtra(CityDetail.CITY_EXTRA, city);
                startActivity(intent);
            }
        });

        View location = findViewById(R.id.location);
        location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TourSpotDetailActivity.this, BaiduMapActivity.class);
                intent.putExtra(TOUR_EXTRA, spot);
                intent.putExtra(CityDetail.CITY_EXTRA, city);
                startActivity(intent);
            }
        });
    }

    @Override
    public void setTitle(CharSequence title) {
        TextView tv = findViewById(R.id.title);
        tv.setText(title);
    }
}
